# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
import pymysql # 用于连接数据库

class DangdangPipeline:
    def process_item(self, item, spider):
        for i in range(0,len(item['title'])):
            title=item["title"][i] # 爬取的结果是一个列表
            link=item["link"][i]
            comment=item["comment"][i]
            connect=pymysql.connect(host="127.0.0.1",user="root",passwd="root",db="datamine",autocommit=True) # 不设置自动提交无法无法插入
            sql="insert into dangdangbook(title,link,comment) values("+"'"+title+"',"+"'"+link+"',"+"'"+comment+"'"+ ")"
            connect.query(sql)
        connect.close()
        return item
